﻿using Interfaces.Validation.Services;
using Microsoft.Practices.EnterpriseLibrary.Validation;

namespace Framework.Validators.User
{
    public class EmailExistValidator : Validator<string>
    {
        private readonly IUserValidationService _service;

        public EmailExistValidator(string messageTemplate, string tag, IUserValidationService service) 
            : base(messageTemplate, tag)
        {
            _service = service;
        }

        protected override string DefaultMessageTemplate
        {
            get { return "Email not exists"; }
        }

        protected override void DoValidate(string objectToValidate, object currentTarget, string key, ValidationResults validationResults)
        {
            if(string.IsNullOrEmpty(objectToValidate)) return;

            if (!_service.IsEmailExists(objectToValidate))
            {
                LogValidationResult(validationResults, DefaultMessageTemplate, currentTarget, key);
            }

        }
    }
}
